package com.yanqu.road.server.manger.config;

import com.yanqu.road.logic.bussiness.config.EstateWarBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.entity.estatewar.EstateBuildingInfo;
import com.yanqu.road.entity.estatewar.EstateFloorBattleResult;
import com.yanqu.road.entity.estatewar.EstateFloorInfo;
import com.yanqu.road.entity.estatewar.UserEstateWarData;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class EstateWarConfigMgr extends TempMgr {

	private static Map<Integer, Map<Integer, EstateFloorInfo>> estateFloorInfoMap;

	private static Map<Integer, EstateBuildingInfo> estateBuildingInfoMap;

	private static Map<Integer, EstateFloorInfo> orderEstateFloorMap;

	private static int maxEstateFloorIndex;

	private static RandomHelper threadRandom = new RandomHelper();

	@Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean stop() {
		return false;
	}

	@Override
	public boolean save() {
		return false;
	}

	@Override
	public boolean reloadConfig() {
		Map<Integer, Map<Integer, EstateFloorInfo>> buildingFloorMap = new ConcurrentHashMap<>();
		Map<Integer, EstateFloorInfo> floorIndexMap = new ConcurrentHashMap<>();
		EstateWarBussiness.getEstateFloorInfoMap(buildingFloorMap, floorIndexMap);
		estateFloorInfoMap = buildingFloorMap;
		orderEstateFloorMap = floorIndexMap;
		estateBuildingInfoMap = EstateWarBussiness.getEstateBuildingInfoMap();
		if (estateFloorInfoMap.size() <= 0) {
			return false;
		}
		if(orderEstateFloorMap.size() <= 0){
			return false;
		}
		initMaxEstateFloorIndex();
		return true;
	}

	@Override
	public boolean reloadData() throws Exception {
		return true;
	}

	private void initMaxEstateFloorIndex(){
		int maxFloorIndex = 0;
		for(EstateFloorInfo estateFloorInfo : orderEstateFloorMap.values()){
			if(estateFloorInfo.getFloorIndex() > maxFloorIndex){
				maxFloorIndex = estateFloorInfo.getFloorIndex();
			}
		}
		maxEstateFloorIndex = maxFloorIndex;
	}

	public static Map<Integer, Map<Integer, EstateFloorInfo>> getEstateFloorInfoMap() {
		return estateFloorInfoMap;
	}

	/**
	 * 获取层数
	 */
	public static int getEstateFloorIndex(int type, int pos){
		for(EstateFloorInfo estateFloorInfo : orderEstateFloorMap.values()){
			if(estateFloorInfo.getType() == type && estateFloorInfo.getPos() == pos){
				return estateFloorInfo.getFloorIndex();
			}
		}
		return 0;
	}

	/**
	 * 获取层数
	 */
	public static int getEstateFloorIndex(UserEstateWarData userEstateWarData){
		int type = 0;
		int pos = 0;
		if(null != userEstateWarData) {
			type = userEstateWarData.getMaxType();
			pos = userEstateWarData.getMaxPos();
		}
		return getEstateFloorIndex(type, pos);
	}

	/**
	 * 获取楼层NPC实力
	 */
	public static BigInteger getNpcAbility(int type, int pos){
		EstateFloorInfo estateFloorInfo = getEstateFloorInfo(type, pos);
		if(null == estateFloorInfo){
			return null;
		}
		BigInteger ability = BigInteger.ZERO;
		for(BigInteger value : estateFloorInfo.getNpcAbilityList()){
			ability = ability.add(value);
		}
		return ability;
	}

	/**
	 * 获取楼层信息
	 */
	public static EstateFloorInfo getEstateFloorInfo(int type, int pos){
		if(estateFloorInfoMap.containsKey(type)){
			return estateFloorInfoMap.get(type).get(pos);
		}
		return null;
	}

	/**
	 * 获取楼层信息
	 */
	public static EstateFloorInfo getEstateFloorInfoByIndex(int index){
		return orderEstateFloorMap.get(index);
	}

	/**
	 * 获取建筑信息
	 */
	public static EstateBuildingInfo getEstateBuildingInfo(int type){
		return estateBuildingInfoMap.get(type);
	}

	public static Map<Integer, EstateBuildingInfo> getEstateBuildingInfoMap(){
		return estateBuildingInfoMap;
	}

	/**
	 * 整个建筑是否NPC
	 */
	public static boolean getIsNpcEstateBuilding(int type){
        EstateBuildingInfo estateBuildingInfo = getEstateBuildingInfo(type);
        if(null != estateBuildingInfo) {
            return estateBuildingInfo.isNpc();
        }
        return true;
	}

	/**
	 * 建筑是否能够膜拜
	 */
	public static boolean getEstateBuildingCanWorship(int type){
		EstateBuildingInfo estateBuildingInfo = getEstateBuildingInfo(type);
		if(null != estateBuildingInfo){
			return estateBuildingInfo.isCanWorship();
		}
		return false;
	}

	/**
	 * 战胜获取楼层奖励
	 */
	public static EstateFloorBattleResult getEstateFloorReward(int type, int pos, UserEstateWarData userEstateWarData){
		if(null == userEstateWarData){
			return null;
		}
		EstateFloorBattleResult battleResult = new EstateFloorBattleResult();
		int index = getEstateFloorIndex(type, pos);
		int maxIndex = getEstateFloorIndex(userEstateWarData.getMaxType(), userEstateWarData.getMaxPos());
		int offset = index - maxIndex;
		if(offset < 0){
			offset = 0;
		}
		battleResult.setRiseFloorCount(offset);
		if(offset > 0){
			Property reward = new Property();
			for(int i = maxIndex + 1; i <= index; i++){
				EstateFloorInfo floorInfo = getEstateFloorInfoByIndex(i);
				reward.addProperty(floorInfo.getFloorReward());
			}
			battleResult.setReward(reward);
		}
		return battleResult;
	}

	public static String getEstateFloorName(int type, int pos, String language){
		EstateFloorInfo floorInfo = getEstateFloorInfo(type, pos);
		if(null == floorInfo){
			return "";
		}
		EstateBuildingInfo estateBuildingInfo = getEstateBuildingInfo(type);
		if(null == estateBuildingInfo){
			return "";
		}
		return MultipleLanguageMgr.getContent(estateBuildingInfo.getName(), language) + floorInfo.getPos();
	}

	public static int getMaxEstateFloorIndex() {
		return maxEstateFloorIndex;
	}

	public static boolean isMaxEstateFloorIndex(int type, int pos){
		int floorIndex = getEstateFloorIndex(type, pos);
		if(floorIndex >= maxEstateFloorIndex){
			return true;
		}
		return false;
	}

	//判断是否是发乔迁贴的楼层(只会返回一张，策划说了不会打一次产生多张，已录音)
	public static EstateFloorInfo getBanquetInvitationFloor(int type,int pos,UserEstateWarData userEstateWarData){
		if(null == userEstateWarData){
			return null;
		}
		int index = getEstateFloorIndex(type, pos);
		int maxIndex = getEstateFloorIndex(userEstateWarData.getMaxType(), userEstateWarData.getMaxPos());
		int offset = index - maxIndex;
		if(offset < 0){
			offset = 0;
		}
		if(offset > 0){
			for(int i = maxIndex + 1; i <= index; i++){
				EstateFloorInfo floorInfo = getEstateFloorInfoByIndex(i);
				if(floorInfo.isInvitation()){
					return floorInfo;
				}
			}
		}
		return null;
	}

	/**
	 * 玩家攻打位置是否超过能打的范围
	 */
	public static boolean isOverCanBattleFloor(int type, int pos, int myType, int myPos, int gapIndex){
		int index = getEstateFloorIndex(type, pos);
		int myIndex = getEstateFloorIndex(myType, myPos);
        int offset = index - myIndex;
        if(offset <= 0){
        	return true;
		}else {
        	return offset > gapIndex;
		}
	}

	public static Property getWorshipReward(UserEstateWarData userEstateWarData){
		int count = 0;
		EstateFloorInfo floorInfo = getEstateFloorInfo(userEstateWarData.getType(), userEstateWarData.getPos());
		if(null != floorInfo){
			for(Map.Entry<Integer, BigInteger> dataEntry : floorInfo.getDailyReward().getGoods().entrySet()){
				count = dataEntry.getValue().intValue();
				break;
			}
			int randomValue = threadRandom.next(ConfigMgr.getEstateWarWorshipRewardParam2(), ConfigMgr.getEstateWarWorshipRewardParam3());
			count = (int) (count * ((double)randomValue / 1000));
		}
		count = count + ConfigMgr.getEstateWarWorshipRewardParam1();
		Property reward = new Property(GameConfig.GAME_ITEM_ESTATE_DAILY_WINE, BigInteger.valueOf(count));
		return reward;
	}

	/**
	* 描述：是否可以换肤的府邸类型
	* 作者：zrq
	* 时间：2022/1/25
	* 参数：
	府邸类型
	* 返回值：
	**/
	public static boolean canChangEstateSkinType(int type){
		EstateBuildingInfo estateBuildingInfo = getEstateBuildingInfo(type);
		if(null == estateBuildingInfo){
			return false;
		}
		if(estateBuildingInfo.getSceneSystemList().size() < 8){
			return false;
		}
		return estateBuildingInfo.getSceneSystemList().get(7) == 1;
	}
}
